package com.knife.oauth.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.knife.cache.redis.constants.RedisConstant;
import com.knife.oauth.infra.cache.v2.KnifeIamUserRedisCache;
import com.knife.oauth.infra.mapper.IamUserMapper;
import com.knife.oauth.security.customer.cache.v2.KnifeIpCache;
import com.knife.oauth.security.customer.mapper.KnifeIpMapper;
import com.knife.redis.KnifeRedisHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： 76875
 * @date： 2022/4/15 星期五 18:00
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Configuration("KnifeOauthRedisConfigure")
public class KnifeOauthRedisConfigure {
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private KnifeRedisHelper knifeRedisHelper;
    @Resource
    private KnifeIpMapper knifeIpMapper;
    @Resource
    private IamUserMapper iamUserMapper;


    @Bean
    public KnifeIpCache knifeIpCache() {
        return new KnifeIpCache(1,
                RedisConstant.OAUTH + ":" + "knifeIpCache",
                () -> knifeIpMapper.selectList(null),
                t->String.valueOf(t.getType()),
                t->String.valueOf(t.getIpAddress()),
                knifeRedisHelper,
                objectMapper
        );
    }

    @Bean
    public KnifeIamUserRedisCache knifeIamUserRedisCache() {
        return new KnifeIamUserRedisCache(1,
                RedisConstant.USER + ":" + "userCache",
                () -> iamUserMapper.selectList(null),
                t->String.valueOf(t.getId()),
                knifeRedisHelper,
                objectMapper
        );
    }

}
